package View;

import java.util.ArrayList;
import java.util.Scanner;

import Controller.VendorController;
import Controller.ProductController;
import Helper.Helper;
import Model.ProductM;
import Model.Product;

public class VendorIndex {
    public static void index(String username){
        Helper.clear();
        System.out.printf("*-----[ 卖家 | %5s ]-----*\n\n", username);

        System.out.println("\t 1. 浏览商品");
        System.out.println("\t 2. 搜索商品");
        System.out.println("\t 3. 添加商品");
        System.out.println("\t 4. 编辑商品");
        System.out.println("\t 5. 删除商品\n");

        System.out.println("\t 6. 注销");

        System.out.println("*---------------------------*");
    }

    public static void listProducts(){
        Helper.listProducts();

        if( Helper.brancher("是否修改商品？", false) ){
            editProduct();
        }
    }

    public static void searchProduct(){
        Helper.searchProduct();

        if( Helper.brancher("是否修改商品？", false) ){
            editProduct();
        }
    }

    public static void addProduct(){
        System.out.println("[ 添加商品 ]\n");
        System.out.print("商品名: ");

        Scanner scanner = new Scanner(System.in);

        String productName = scanner.next();
        while( productName.matches("[`~!@#$%^&*()+=|{}':;,\\[\\].<>/?！￥…（）—]+") ){
            System.err.println("商品名只能由字母数字下划线组成，且长度不超过50字节！");
            System.out.print("请重新输入商品名： ");

            productName = scanner.next();
        }

        System.out.print("价格: ");
        String tempPrice = scanner.next();
        while( !tempPrice.matches("[0-9.]+") || Float.parseFloat(tempPrice)<0 ){
            System.err.println("请输入正确的价格！");
            System.out.print("请重新输入价格： ");

            tempPrice = scanner.next();
        }
        float price = Float.parseFloat(tempPrice);

        System.out.print("数量: ");
        String tempAmount = scanner.next();
        while( !tempAmount.matches("[0-9]+") || Integer.parseInt(tempAmount)<0 ){
            System.err.println("请输入正确的数量！");
            System.out.print("请重新输入数量： ");

            tempAmount = scanner.next();
        }
        int amount = Integer.parseInt(tempAmount);

        System.out.print("是否上架(0: 否,1: 是): ");
        String tempStatus = scanner.next();
        while( !tempStatus.matches("[0-1]") || Integer.parseInt(tempStatus)<0 ){
            System.err.println("请输入0或者1(0: 否,1: 是)！");
            System.out.print("请重新选择是否上架： ");

            tempStatus = scanner.next();
        }
        int status = Integer.parseInt(tempStatus);

        if( !Helper.brancher("确定？", true) ){
            return;
        }

        boolean s = VendorController.addProduct(productName, price, amount, status);

        if( s ){
            System.out.println("添加成功！");
        }else{
            System.err.println("添加失败!");
        }

        Helper.waitter();
    }

    public static void editProduct(){
        System.out.println("[ 修改商品 ]\n");
        System.out.print("商品编号: ");
        String productName;
        Float price;
        int amount;

        Scanner scanner = new Scanner(System.in);

        String tempId = scanner.nextLine();
        while( !tempId.matches("[0-9]+") || Integer.parseInt(tempId)<0 ){
            System.err.println("请输入正确的编号！");
            System.out.print("请重新输入编号： ");

            tempId = scanner.nextLine();
        }
        int productId = Integer.parseInt(tempId);

        ProductM productM = new ProductM();
        ArrayList<Product> pArr = productM.find(productId);

        if( pArr.size() == 0 ){
            System.err.println("不存在编号为"+productId+"的商品！");
            Helper.waitter();
        } else if (!pArr.get(0).vendor.equals(Helper.getSession(0))) {
            System.err.println("您并没有权限编辑此商品！");
            Helper.waitter();
        } else {
            Product p = pArr.get(0);
            System.out.println("正在编辑： 【 "+p.name+" 】");

            System.out.print("商品名[ "+p.name+" ]: ");
            productName = scanner.nextLine();
            if( !productName.equals("") ){
                while( productName.matches("[`~!@#$%^&*()+=|{}':;,\\[\\].<>/?！￥…（）—]+") ){
                    System.err.println("商品名只能由字母数字下划线组成，且长度不超过50字节！");
                    System.out.print("请重新输入商品名： ");

                    productName = scanner.nextLine();
                }
            }else{
                productName = p.name;
            }

            System.out.print("价格[ "+p.price+" ]: ");
            String tempPrice = scanner.nextLine();
            if( !tempPrice.equals("") ){
                while( !tempPrice.matches("[0-9.]+") || Float.parseFloat(tempPrice)<0 ){
                    System.err.println("请输入正确的价格！");
                    System.out.print("请重新输入价格： ");

                    tempPrice = scanner.nextLine();
                }
                price = Float.parseFloat(tempPrice);
            }else{
                price = p.price;
            }

            System.out.print("数量[ "+p.amount+" ]: ");
            String tempAmount = scanner.nextLine();
            if( !tempAmount.equals("") ){
                while( !tempAmount.matches("[0-9]+") || Integer.parseInt(tempAmount)<0 ){
                    System.err.println("请输入正确的数量！");
                    System.out.print("请重新输入数量： ");

                    tempAmount = scanner.nextLine();
                }
                amount = Integer.parseInt(tempAmount);
            }else{
                amount = p.amount;
            }

            System.out.print("是否上架(0: 否,1: 是): ");
            String tempStatus = scanner.next();
            while( !tempStatus.matches("[0-1]") || Integer.parseInt(tempStatus)<0 ){
                System.err.println("请输入0或者1(0: 否,1: 是)！");
                System.out.print("请重新选择是否上架： ");

                tempStatus = scanner.next();
            }
            int status = Integer.parseInt(tempAmount);

            if( !Helper.brancher("确定？", true) ){
                return;
            }

            boolean s = VendorController.editProduct(productId, productName, price, amount, status);

            if( s ){
                System.out.println("修改成功！");
            }else{
                System.err.println("修改失败!");
            }

            Helper.waitter();
        }
    }

    public static void removeProduct(){
        System.out.println("[ 删除商品 ]\n");
        System.out.print("商品编号: ");

        Scanner scanner = new Scanner(System.in);

        String tempId = scanner.nextLine();
        while( !tempId.matches("[0-9]+") || Integer.parseInt(tempId)<0 ){
            System.err.println("请输入正确的编号！");
            System.out.print("请重新输入编号： ");

            tempId = scanner.nextLine();
        }
        int productId = Integer.parseInt(tempId);

        ProductM productM = new ProductM();
        ArrayList<Product> pArr = productM.find(productId);

        if( pArr.size() == 0 ){
            System.err.println("不存在编号为"+productId+"的商品！");
            Helper.waitter();
        } else if (!pArr.get(0).vendor.equals(Helper.getSession(0))) {
            System.err.println("您并没有权限删除此商品！");
            Helper.waitter();
        } else {
            Product p = pArr.get(0);
            System.out.println("正在删除： 【 "+p.name+" 】");

            if( !Helper.brancher("确定？", false) ){
                return;
            }

            boolean status = VendorController.removeProduct(productId);

            if( status ){
                System.out.println("删除成功！");
            }else{
                System.err.println("删除失败!");
            }

            Helper.waitter();
        }
    }
}
